﻿// -----------------------------------------------------------------------
// <copyright file="ClipboardViewModel.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TouchExplorer.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
using System.Collections.ObjectModel;
    using System.Windows;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ClipboardViewModel : INotifyPropertyChanged
    {
        public TouchFileElementBase this[string path]
        {
            get
            {
                return ClipboardElements.FirstOrDefault(elt => elt.FullPath == path);
            }
        }

        private bool _hasElements;

        public bool HasElements
        {
            get
            {
                return this._hasElements;
            }
            set
            {
                this._hasElements = value;
                this.OnPropertyChanged("HasElements");
            }
        }

        private int _eltCount;

        public int Count
        {
            get
            {
                return this._eltCount;
            }
            set
            {
                this._eltCount = value;
                this.OnPropertyChanged("Count");
            }
        }

        private bool _isCutMode;

        public bool IsInCutMode
        {
            get
            {
                return this._isCutMode;
            }
            set
            {
                this._isCutMode = value;
                this.OnPropertyChanged("IsInCutMode");
                this.OnPropertyChanged("IsInCopyMode");
            }
        }

        public bool IsInCopyMode
        {
            get
            {
                return !_isCutMode;
            }
            set
            {}
        }

        private ObservableCollection<TouchFileElementBase> _clipboardElements;

        public ObservableCollection<TouchFileElementBase> ClipboardElements
        {
            get
            {
                return this._clipboardElements;
            }
            set
            {
                this._clipboardElements = value;
                this.OnPropertyChanged("ClipboardElements");
            }
        }

        private void CheckVisibility()
        {
            Count = ClipboardElements.Count;
            if (Count > 0)
                HasElements = true;
            else
                HasElements = false;
        }

        public ClipboardViewModel()
        {
            HasElements = false;
            _clipboardElements = new ObservableCollection<TouchFileElementBase>();
            _clipboardElements.CollectionChanged += (sender, arg) => this.CheckVisibility();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

        public void AddCopy(params TouchFileElementBase[] elements)
        {
            AddCopy((IEnumerable<TouchFileElementBase>)elements);
        }

        public void AddCopy(IEnumerable<TouchFileElementBase> elements)
        {
            if (IsInCutMode)
            {
                Clear();
                IsInCutMode = false;
            }

            this.AddElements(elements, elt =>
                {
                    elt.IsCopyed = true;
                    elt.IsCutted = false;
                });
        }

        public void AddCut(params TouchFileElementBase[] elements)
        {
            this.AddCut((IEnumerable<TouchFileElementBase>)elements);
        }

        public void AddCut(IEnumerable<TouchFileElementBase> elements)
        {
            if (IsInCopyMode)
            {
                Clear();
                IsInCutMode = true;
            }

            this.AddElements(elements, elt =>
                {
                    elt.IsCopyed = false;
                    elt.IsCutted = true;
                });
        }

        private void AddElements(IEnumerable<TouchFileElementBase> elements, Action<TouchFileElementBase> eltAct)
        {
            foreach (var touchFileElementBase in elements)
            {
                touchFileElementBase.IsSelected = false;
                if (touchFileElementBase.Exists && !ClipboardElements.Contains(touchFileElementBase))
                {
                    if (eltAct != null)
                    {
                        eltAct(touchFileElementBase);
                    }
                    this.ClipboardElements.Add(touchFileElementBase);
                }
            }
        }

        public void Clear()
        {
            foreach (var touchFileElementBase in ClipboardElements)
            {
                touchFileElementBase.IsCutted = false;
                touchFileElementBase.IsCopyed = false;
            }
            ClipboardElements.Clear();            
        }

        internal void Remove(TouchFileElementBase touchFileElementBase)
        {
            touchFileElementBase.IsCutted = false;
            touchFileElementBase.IsCopyed = false;
            ClipboardElements.Remove(touchFileElementBase);
        }

        public bool Has(string path)
        {
            return ClipboardElements.Any(e => e.FullPath == path);
        }

        public void Apply(string targetDir)
        {
            MessageBox.Show("This is supposed to copy or cut elements in the current directory, but is not yet implemented...");
            this.Clear();
        }
    }
}
